package com.hospital.wx.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.hospital.wx.common.CommonResult;
import com.hospital.wx.controller.form.*;
import com.hospital.wx.pojo.wx.PaymentRequest;
import com.hospital.wx.service.AppointmentOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/*
 * 就诊预约订单管理接口
 * */
@RestController
@RequestMapping("/patient/appointment/order")
@Tag(name = "AppointmentOrderController", description = "患者就诊预约订单接口管理")
@Slf4j
public class AppointmentOrderController {

    @Autowired
    private AppointmentOrderService appointmentOrderService;

    @PostMapping("/create")
    @Operation(summary = "创建订单信息")
    public CommonResult createOrder(@RequestBody @Valid CreateAppointmentOrderForm form) {
        try {
            log.info("创建支付订单的请求参数: {}", JSON.toJSONString(form));
            Map<String, Object> param = BeanUtil.beanToMap(form);
            Map result = appointmentOrderService.createAppointmentOrder(param);
            return CommonResult.ok().put(CommonResult.RETURN_RESULT, result);
        } catch (Exception e) {
            log.error("创建订单过程中出现异常", e);
            return CommonResult.failed("创建订单过程中出现异常");
        }
    }

    @PostMapping("/detail")
    @Operation(summary = "获取订单信息")
    public CommonResult getOrderDetail(@Valid @RequestBody SelectOrderDetailByIdForm form) {
        try {
            HashMap map = appointmentOrderService.selectOrderDetailById(form.getOrderId());
            return CommonResult.ok().put(CommonResult.RETURN_RESULT, map);
        } catch (Exception e) {
            log.error("获取订单详情过程中出现异常", e);
            return CommonResult.failed("获取订单详情过程中出现异常");
        }
    }


    @PostMapping("/paySuccess/callback")
    @Operation(summary = "支付成功后更新订单状态")
    public CommonResult updateOrderStatus(@RequestBody @Valid UpdateOrderStatusForm form) {
        try {
            Map param = BeanUtil.beanToMap(form);
            appointmentOrderService.updateOrderStatus(param);
            return CommonResult.ok();
        } catch (Exception e) {
            log.error("更新订单状态过程中出现错误", e);
            return CommonResult.failed("跟新订单状态过程中出现啊错误");
        }
    }

    @PostMapping("/cancel")
    @Operation(summary = "取消订单")
    public CommonResult cancelOrder(@RequestBody @Valid CancelOrderForm form) {
        try {
            appointmentOrderService.cancelOrder(form.getOrderId());
            return CommonResult.ok();
        } catch (Exception e) {
            log.error("更新订单状态过程中出现错误", e);
            return CommonResult.failed("跟新订单状态过程中出现啊错误");
        }
    }


    @PostMapping("/list")
    @Operation(summary = "查询就这预约的订单集合")
    public CommonResult getAppointmentOrderList(@RequestBody @Valid GetOrderListForm form) {
        try {
            ArrayList<HashMap> list = appointmentOrderService.getOrderList(form.getPatientId());
            return CommonResult.ok().put("rows", list);
        } catch (Exception e) {
            log.error("查询就诊预约过程中出现异常", e);
            return CommonResult.failed("查询就诊预约过程中出现异常");
        }
    }
}
